package com.ybkj.daijia.server.event.listener;

import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.server.event.model.OrderAutoPaidanEvent;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Order;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.SmsTempletService;
import com.ybkj.daijia.server.websocket.WebSocketSend;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class OrderAutoPaidanEventListener implements ApplicationListener<OrderAutoPaidanEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(OrderAutoPaidanEventListener.class);

    @Autowired
    private SmsTempletService smsTempletService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private CompanyService companyService;


    @Override
    @Async
    public void onApplicationEvent(OrderAutoPaidanEvent event) {

        Order order = (Order) event.getSource();
        logger.debug("order autopaidan event:{}", order.getOrderNumber());

        //推送数据到浏览器
        JSONObject jsonObject = new JSONObject();
        jsonObject
            .put("message", "订单号：" + order.getOrderNumber() + "自动派单失败，原因：该派单时间点无可用服务人员，请手动派单");
        jsonObject.put("type", "autoPaidan");
        Company mainCompany = companyService.getMain();
        WebSocketSend.boardcastToCompany(jsonObject, order.getCompanyId(), mainCompany.getId());

    }


}
